package com.evil.common.activiti.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Optional;

/**
 * 流程线 类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum SequenceFlowTypeEnum {

    /**
     * 流程线 类型
     */
    COMPLETE("COMPLETE", "流转线"),
    FALLBACK("FALLBACK", "回退线"),
    REFUSE("REFUSE", "拒绝线"),
    ;

    @Getter
    private final String type;
    @Getter
    private final String name;

    public static Optional<SequenceFlowTypeEnum> findByType(String type) {
        if (StringUtils.isBlank(type)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type.equals(type)).findFirst();
    }

    public static SequenceFlowTypeEnum isExistByType(String type) {
        return SequenceFlowTypeEnum.findByType(type).orElseThrow(SequenceFlowTypeEnum::getBusinessException);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "流程线类型参数错误");
    }

    /**
     * 是否是 流转线
     *
     * @return boolean
     */
    public boolean isComplete() {
        return COMPLETE.type.equals(this.type);
    }
}
